package com.ddwl.goods.service;

import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.SignHistDao;
import com.ddwl.goods.model.SignHist;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.SignHistBo;
import com.ddwl.schema.util.DateUtil;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.SignConfigVo;
import com.ddwl.schema.vo.goods.SignHistVo;
import com.ddwl.schema.vo.goods.SignListVo;
import com.ddwl.schema.vo.goods.SignVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/10/20 15:42
 */
@Service
public class SignHistService {

    @Resource
    private SignHistDao signHistDao;
    @Resource
    private SignConfigService signConfigService;

    public void add(SignHistBo signHistBo) {
        SignHist entity = BeanConverUtils.convertBean(signHistBo, SignHist.class);
        signHistDao.insert(entity);
    }

    public PageVo<SignHistVo> page(PageBo<SignHistBo> bo) {
        PageHelper.startPage(bo.getPage(), bo.getPageSize(), MySqlUtil.orderBy(bo.getSort(), bo.isAsc()));
        PageInfo<SignHist> info = new PageInfo(signHistDao.getList(bo.getParam()));
        List<SignHistVo> list = BeanConverUtils.convertBeanList(info.getList(),SignHistVo.class);
        return new PageVo<>(info.getTotal(),bo.getPage(),bo.getPageSize(),list);
    }

    public SignVo list(String userId) {
        List<SignConfigVo> signConfigVoList=signConfigService.list();
        ParamException.isNull(signConfigVoList,"签到奖励未准备，请稍后");
        String dayOfWeek=DateUtil.getDayOfWeek(Instant.now());
        Integer weekCode = GoodsEnum.WeekEnum.getCodeBYDay(dayOfWeek);
        List<String> weekDate= DateUtil.currentWeek();
        List<SignHist> signHistList= signHistDao.getThisWeek(userId,weekDate);
        SignVo signVo = new SignVo();
        signVo.setSignDays(null == signHistList ? 0 : signHistList.size());
        List<SignListVo> list = new ArrayList<>();
        for (int i = 0; i < signConfigVoList.size(); i++) {
            SignConfigVo vo = signConfigVoList.get(i);
            SignListVo slVo = new SignListVo();
            slVo.setWeekCode(vo.getWeekCode());
            slVo.setDayOfWeek(vo.getDayOfWeek());
            slVo.setRewardType(vo.getRewardType());
            slVo.setRewardNum(vo.getRewardNum());
            slVo.setReissueReward(vo.getReissueReward());
            slVo.setContinueReward(vo.getContinueReward());
            slVo.setDate(DateUtil.stringToInstant(weekDate.get(i)));
            if (vo.getDayOfWeek().equals(dayOfWeek)) {
                slVo.setToday(GoodsEnum.SignEnum.ONE.getCode());
            } else {
                slVo.setToday(0);
            }
            for (SignHist sh : signHistList) {
                if (vo.getDayOfWeek().equals(sh.getDayOfWeek())) {
                    slVo.setId(sh.getId());
                    slVo.setContinueDays(sh.getContinueDays());
                    slVo.setSignDays(sh.getSignDays());
                    slVo.setSignDate(sh.getSignDate());
                    slVo.setSignType(GoodsEnum.SignEnum.ONE.getCode());
                    slVo.setGmtCreated(sh.getGmtCreated());
                    slVo.setGmtModified(sh.getGmtModified());
                    slVo.setSignReward(sh.getSignReward());
                }
            }
            if (vo.getWeekCode().equals(GoodsEnum.WeekEnum.SUN.getCode())) {
                signVo.setContinueReward(vo.getContinueReward());
                if(signVo.getSignDays()==7){
                    slVo.setGiftStatus(1);
                }else{
                    if(GoodsEnum.SignEnum.ONE.equals(slVo.getSignType())){
                        slVo.setGiftStatus(2);
                    }else{
                        slVo.setGiftStatus(0);
                    }
                }
            }
            if(null == slVo.getSignType()){
                if (vo.getWeekCode().compareTo(weekCode) >= 0) {
                    slVo.setSignType(0);
                }
                if (vo.getWeekCode().compareTo(weekCode) < 0) {
                    slVo.setSignType(2);
                }
            }
            if (null == slVo.getSignType() && vo.getWeekCode().compareTo(weekCode) < 0) {
                slVo.setSignType(2);
            }
            list.add(slVo);
        }
        signVo.setSignList(list);
        return signVo;
    }

    public SignHistVo getByUserIdAndSignDate(String userId, String formatDate) {
        return BeanConverUtils.convertBean(signHistDao.getByUserIdAndSignDate(userId,formatDate), SignHistVo.class);
    }
}
